<?php
/**
* productmodel.php
*
* Create on 2014-12-27
* Create by TT
*/
namespace haibao\cms\business;

use haibao\frame\data\query\Condition;

use \haibao\cms\data\ProductModel as ProductModelData;
use \haibao\cms\model\filter\ProductModel as ProductModelFilter;

class ProductModel extends \haibao\cms\business\Base{
	
    public static function getProductPicById($productId){
        $productModelData = new ProductModelData();
        return $productModelData->getProductPicById($productId);
    }
    
	/**
	 * 得到单品下的所有实体
	 * @param \haibao\cms\model\filter\ProductModel $filter
	 */
	public static function getModelsByProductId($filter){
		$productModelData = new ProductModelData();
		return $productModelData->getModelsByProductId($filter);
	}
	
	/**
	 * 得到某一单品实体
	 * @param \haibao\cms\model\filter\ProductModel $filter
	 */
	public static function getOneModel($filter){
		$productModelData = new ProductModelData();
		return $productModelData->getOneModel($filter);
	}
	
	/**
	 * 添加单品模型
	 * @param \haibao\cms\model\data\ProductModel $model	单品模型实体
	 */
	public static function addProduct($model){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
		if(empty($model)){
			throw new BusinessException('单品模型不能为空',self::DATA_EMPTY);
		}
		if(empty($model->ProductId)){
			throw new BusinessException('单品ID不能为空',self::DATA_ERROR_PRODUCT_ID);
		}
		$model->CreateBy = self::getCurrentUser()->Id;
		$model->CreateTime = new \DateTime();
		
		$productModelData = new ProductModelData();
		$productModelData->addProduct($model,self::getCurrentUser()->UserName);
  		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_ADD, $model->toArray(false));
	}
	/**
	 * 批量修改单品模型
	 * @param array $ids 单品模型ids
	 * @param array $arrset 需要修改的字段
	 */
	public static function batchUpdateProduct($ids,$arrSet,$modelPropertyModes = array()){
		self::checkOperation(self::FUNC_CODE_PRODUCT_UPDATE);
		
		if(empty($arrSet)){
			throw new BusinessException('需要修改的数据不能为空',self::DATA_EMPTY);
		}
		if(empty($ids)){
			throw new BusinessException('单品模型ID不能为空',self::DATA_ERROR_PRODUCT_MODEL_ID);
		}
		
		$productModelData = new ProductModelData();
		$productModelData->batchUpdateProduct($ids, $arrSet,$modelPropertyModes,self::getCurrentUser()->UserName);
		
		$arrSet['id'] = implode(',', $ids);		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_UPDATE, $arrSet);
	}
	/**
	 * 修改单品模型
	 * @param int $id	单品模型ID
	 * @param array $arrSet 需要修改的字段
	 * @param array(\haibao\cms\model\data\ProductModelProperty) $modelPropertyModes 需要属性实体
	 */
	public static function updateProduct($id,$arrSet,$modelPropertyModes = array()){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
		if(empty($id)){
			throw new BusinessException('单品模型ID不能为空',self::DATA_ERROR_PRODUCT_MODEL_ID);
		}
		if(empty($arrSet)){
			throw new BusinessException('需要修改的数据不能为空',self::DATA_EMPTY);
		}
		$productModelData = new ProductModelData();
		$productModelData->updateProduct($id, $arrSet,$modelPropertyModes,self::getCurrentUser()->UserName);
		
		$arrSet['Id'] = $id;
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_UPDATE, $arrSet);
	}
	
	public static function updateStatusById($id,$status = \haibao\cms\model\data\product::STATUS_ENABLE){
		self::checkOperation(self::FUNC_CODE_PRODUCT_DEL);
		if(empty($id)){
			throw new BusinessException('单品模型ID不能为空',self::DATA_ERROR_PRODUCT_MODEL_ID);
		}
		$productModelData = new ProductModelData();
		$productModelData->updateStatusById($id,$status);
		
		$arrSet['Id'] = $id;
		$arrSet['Status'] = $status;
		$arrSet['CreateBy'] = self::getCurrentUser ()->Id;
		$arrSet['UpdateTime'] = new \DateTime();
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_DEL, $arrSet);
	}
	
	/**
	 * 删除产品模型
	 * @param array(int) $ids
	 */
	public static function delProduct($ids){
		self::checkOperation(self::FUNC_CODE_PRODUCT_DEL);		
		if(empty($ids)){
			throw new BusinessException('单品模型ID不能为空',self::DATA_ERROR_PRODUCT_MODEL_ID);
		}
		$productModelData = new ProductModelData();
		$productModelData->delProduct($ids,self::getCurrentUser()->UserName);
		
		$arrSet['Id'] = $ids;
		$arrSet['CreateBy'] = self::getCurrentUser ()->Id;
		$arrSet['UpdateTime'] = new \DateTime();
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_DEL, $arrSet);
	}
	/**
	 * 获取产品模型
	 *@param \haibao\cms\model\filter\ProductModelFilter $filter
	 */
	public static function getProductModel($filter){
		$productModelData = new ProductModelData();
		return $productModelData->getAll($filter);
	}	
	/**
	 * 根据产品ID获取老产品数据Id
	 */
	public static function getOldProductIds($productIds){
		
		$data = new ProductModelData();
		return $data->getAllDataMigrationMap($productIds);
	}
	/**
	 * 通过产品ID查看单品中是否有产品名称
	 */
	public static function hasProductModelName($productId){
		$result = true;
		$productModelFilter = new ProductModelFilter();
		$productModelFilter->where(ProductModelFilter::CONDITION_FIELD_NAME_PRODUCT_ID, Condition::CONDITION_EQUAL, $productId);
		$productModelFilter->where(ProductModelFilter::CONDITION_FIELD_NAME_PRODUCT_NAME, Condition::CONDITION_EQUAL, '');
		if(self::getProductModel($productModelFilter)){
			$result = false;
		}
		return $result;
	}
}